package other.model01;
/**
 * @ClassName: Singleton
 * @Description: ThreadLocal方式解决DCL存在问题
 * @Version: v1.0.0
 * @Author: tang
 * @Date: 2022/06/06
 * Modification History:
 * Date             Author      Version     Description
 * -------------------------------------------------------------
 * 2022/06/06      tang        v1.0.0      创建
 */
public class Singleton {
	private Singleton(){}
	
	private static ThreadLocal<Singleton> threadLocal = new ThreadLocal<>();
	private static Singleton instance;
	
	public static Singleton getInstance(){
		if (threadLocal.get()==null){
			synchronized (Singleton.class){
				if (instance == null){
					instance = new Singleton();
				}
			}
			threadLocal.set(instance);
		}
		return instance;
	}
	/*
	* threadlocal避开了DCL的问题，但是却增大了内存开销，因为
	* threadlocal本质上是用一个hashmap来管理的这些变量，键为线程对象，值为该线程对应的局部变量副本的值
	* */
}
